#include "orderdao.h"
#include"../utils/database.h"
#include"QSqlQuery"
#include<QSqlRecord>
#include<QVariant>
#include<QSqlError>
#include<QDebug>

/*
 * edit by raoxiaoyang
 * test by raoxiaoyang
 */

OrderDao::OrderDao()
{
    QSqlQuery result=QSqlQuery(*DataBase::getInstance());
}

//订单保存到数据库中
void OrderDao::insert(Bill bill)
{
    /*
     * 将提交的订单保存到数据库里面
     * id不用插入，是自动生成的
     *
    */
    QString sql = "insert into Bill(order_id,username,window_id,price,status,description) value( %1,'%2',%3,%4,%5,'%6')";
    sql=sql.arg(0)
           .arg(QString::fromStdString(bill.username))
           .arg(bill.window_id)
           .arg(bill.price)
           .arg(bill.status)
           .arg(QString::fromStdString(bill.description));
    if(result.exec(sql))
        return;
    else{
        qDebug()<<result.lastError().text();
    }
    return;

}

//查询某个时间段内的指定窗口(id)订单信息
void OrderDao::selectDuring(WindowInfo win,QDate start,QDate end,vector<Bill> &bills)
{
    QString sql = "select * from Bill where window_id=? and ?<=tradingTime and ?>=tradingTime";
    result.prepare(sql);
    result.addBindValue(win.id);
    result.addBindValue(start);
    result.addBindValue(end);
    if(!result.exec())
        return;
    while(result.next()){
        Bill bi;
        bi.order_id = result.value("order_id").toInt();
        bi.username = result.value("username").toString().toStdString();
        bi.window_id = result.value("window_id").toInt();
        bi.price = result.value("price").toDouble();
        bi.tradingTime = result.value("tradingTime").toString().toStdString();
        bi.status = result.value("status").toInt();
        bi.description = result.value("description").toString().toStdString();
        bills.push_back(bi);
    }
    return;
}


//struct Bill
//{
//    string order_id;//内部订单id
//    string username;//商家用户名（电话号码）
//    int window_id;//窗口id
//    double price;//价格
//    string tradingTime;//交易时间
//    int status;//交易状态
//    string description;//订单详细信息

//    const static int STATUS_succeed=0;//交易成功
//    const static int STATUS_failed=1;//交易失败
//};
